import {CodeUtil} from "../frame/codeAdapter/CodeUtil";
import {Logger} from "../frame/apiModel/config/Logger";
import MathUtil from "../frame/codeAdapter/MathUtil";

/**
 * 根据key加密
 */
/**
 *
 * @param keys
 * @param str
 * @param split 不传分隔符的情况下将无法解密，可以用于加密后的字符串对比
 * @param indexSplit
 */
export function encrypted(keys: number[],str: string, split: string, indexSplit: string) {
    let total2str = "";
    const arr: number[] = [];
    const encryptArr = [];
    for (let i = 0; i < str.length; i++) {
        const encryptIndex = i % keys.length;
        const encryptNum = keys[encryptIndex];
        encryptArr.push(encryptNum);
    }
    for (let i = 0; i < str.length; i++) {
        const num10 = str.charCodeAt(i);  ///< 以10进制的整数返回 某个字符 的unicode编码
        const ele = num10;
        arr.push(ele);
    }
    let newArr = [];
    let lastArr = [];
    const fun = function (arr: number[], newArr: string[], encryptArr: number[], lastArr: string[]) {
        for (let i = 0; i < arr.length; i++) {
            let ele = arr[i];
            const encryptNum = encryptArr[i];
            ele = ele * encryptNum;
            if (ele % 3 === 1) {
                lastArr.push(ele + '');
                newArr.push(indexSplit);
            } else {
                newArr.push(ele + '');
            }
        }
    }
    for (let i = 0; i < 1; i ++) {
        fun(arr, newArr, encryptArr, lastArr);
        newArr = newArr.reverse();
    }
    newArr.push(...lastArr)
    total2str = newArr.join(split);
    // console.log("加密后:" + total2str);
    return total2str;
}

/**
 * 根据key解密
 */


export function decrypted(keys: number[],str: string, split: string, indexSplit: string) {
    //秘钥-- ^ 运算后，为补码。  秘钥会转化成为1 0 二进制数。
    let goal = "";
    const arr = str.split(split);
    const encryptArr = [];
    for (let i = 0; i < str.length; i++) {
        const encryptIndex = i % keys.length;
        const encryptNum = keys[encryptIndex];
        encryptArr.push(encryptNum);
    }
    let lastArr = [], newArr = [];
    const fun = function (arr: string[], newArr: number[], encryptArr: number[], lastArr: number[]) {
        for (let i = arr.length - 1; i >= 0; i--) {
            let ele = arr[i];
            const numEle = MathUtil.toNumber(ele);
            if (numEle && numEle % 3 === 1) {
                lastArr.push(numEle);
            } else {
                if (ele === indexSplit) {
                    const lastEle = lastArr.splice(lastArr.length - 1, 1);
                    newArr.push(...lastEle);
                } else {
                    newArr.push(numEle);
                }
            }
        }
    }
    for (let i = 0; i < 1; i ++) {
        fun(arr, newArr, encryptArr, lastArr);
    }
    for(let i = 0; i < newArr.length; i++){
        const num = newArr[i];
        const encryptNum = encryptArr[i];
        const newNum = num / encryptNum;
        // Logger.toastError(newNum)
        goal = `${goal}${String.fromCharCode(newNum)}`; ///< 将10进制的unicode编码, 转换成对应的unicode字符
    }
    return goal;
}